﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using HarmonyLib;
using TaiwuModdingLib.Core.Plugin;
using UnityEngine;
using FrameWork;
using GameData.GameDataBridge;
using GameData.Domains;
using GameData.Domains.Taiwu;
using Config;

namespace FightBoss
{
    [PluginConfig("快速演武v2", "HMS Unicorn", "0.0")]
    public class ModMain : TaiwuRemakePlugin
    {
        Harmony harmony;
        public static ModMono ModMono;
        public override void Dispose()
        {
            if (harmony != null)
            {
                harmony.UnpatchSelf();
            }
        }

        public override void Initialize()
        {
            harmony = Harmony.CreateAndPatchAll(typeof(ModMain));
            ModMain.ModMono = new GameObject("快速演武插件").AddComponent<ModMono>();
            Debug.Log("快速演武插件 Initialize");
        }
    }

    public class ModMono : MonoBehaviour
    {
        static HotKeyCommand alt1 = new HotKeyCommand(201, 10201, KeyCode.Alpha1, KeyCode.LeftAlt);
        static HotKeyCommand alt2 = new HotKeyCommand(202, 10202, KeyCode.Alpha2, KeyCode.LeftAlt);
        static HotKeyCommand alt3 = new HotKeyCommand(203, 10203, KeyCode.Alpha3, KeyCode.LeftAlt);
        static HotKeyCommand alt4 = new HotKeyCommand(204, 10204, KeyCode.Alpha4, KeyCode.LeftAlt);
        static HotKeyCommand alt5 = new HotKeyCommand(205, 10205, KeyCode.Alpha5, KeyCode.LeftAlt);
        static HotKeyCommand alt6 = new HotKeyCommand(206, 10206, KeyCode.Alpha6, KeyCode.LeftAlt);
        static HotKeyCommand alt7 = new HotKeyCommand(207, 10207, KeyCode.Alpha7, KeyCode.LeftAlt);
        static HotKeyCommand alt8 = new HotKeyCommand(208, 10208, KeyCode.Alpha8, KeyCode.LeftAlt);
        static HotKeyCommand alt9 = new HotKeyCommand(209, 10209, KeyCode.Alpha9, KeyCode.LeftAlt);
        static HotKeyCommand alt0 = new HotKeyCommand(200, 10200, KeyCode.Alpha0, KeyCode.LeftAlt);
        static HotKeyCommand altbq = new HotKeyCommand(210, 10210, KeyCode.BackQuote, KeyCode.LeftAlt);
        private void ShowButtonSheet(int difficulty)
        {
            UIManager.Instance.HideUI(UIElement.ButtonSheet);
            bool canFightWithBoss = !(
                UIElement.Combat.Exist || UIElement.CatchCricket.Exist || UIElement.CricketCombat.Exist || UIElement.LifeSkillBattle.Exist
                );
            List<SheetButtonInfo> info = new List<SheetButtonInfo> {
                    new SheetButtonInfo($"剑冢 精纯 {difficulty + 1}", null, false),
                    new SheetButtonInfo("[莫女衣] 莫女", () => { GMFunc.FightWithBoss(48  + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[伏邪铁] 大岳", () => { GMFunc.FightWithBoss(57  + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[大玄凝] 九寒", () => { GMFunc.FightWithBoss(66  + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[凤凰茧] 金凰", () => { GMFunc.FightWithBoss(75  + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[焚神炼] 衣以", () => { GMFunc.FightWithBoss(84  + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[解龙魄] 卫起", () => { GMFunc.FightWithBoss(93  + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[溶尘隐] 以向", () => { GMFunc.FightWithBoss(102 + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[囚魔木] 血枫", () => { GMFunc.FightWithBoss(111 + difficulty); }, canFightWithBoss),
                    new SheetButtonInfo("[鬼神霞] 术方", () => { GMFunc.FightWithBoss(120 + difficulty); }, canFightWithBoss),
                };
            UIElement.ButtonSheet.SetOnInitArgs(EasyPool.Get<ArgumentBox>()
                .SetObject("ButtonInfos", info)
            );
            UIManager.Instance.ShowUI(UIElement.ButtonSheet);
        }
        public void Update()
        {
            if (this.CheckKey(alt1))
            {
                ShowButtonSheet(0);
            }
            else if (this.CheckKey(alt2))
            {
                ShowButtonSheet(1);
            }
            else if (this.CheckKey(alt3))
            {
                ShowButtonSheet(2);
            }
            else if (this.CheckKey(alt4))
            {
                ShowButtonSheet(3);
            }
            else if (this.CheckKey(alt5))
            {
                ShowButtonSheet(4);
            }
            else if (this.CheckKey(alt6))
            {
                ShowButtonSheet(5);
            }
            else if (this.CheckKey(alt7))
            {
                ShowButtonSheet(6);
            }
            else if (this.CheckKey(alt8))
            {
                ShowButtonSheet(7);
            }
            else if (this.CheckKey(alt9))
            {
                ShowButtonSheet(8);
            }
            else if (this.CheckKey(altbq))
            {
                UIManager.Instance.HideUI(UIElement.ButtonSheet);
                List<SheetButtonInfo> info = new List<SheetButtonInfo> {
                        new SheetButtonInfo("战斗命令", null, false),
                        new SheetButtonInfo("认输", () => { GameDataBridge.AddMethodCall(-1, 8, 24); }, UIElement.Combat.Exist),
                        new SheetButtonInfo("劝退", () => { GameDataBridge.AddMethodCall(-1, 8, 23); }, UIElement.Combat.Exist),
                    };
                UIElement.ButtonSheet.SetOnInitArgs(EasyPool.Get<ArgumentBox>()
                    .SetObject("ButtonInfos", info)
                );
                UIManager.Instance.ShowUI(UIElement.ButtonSheet);
            }
        }

        private bool CheckKey(HotKeyCommand keyCommand)
        {
            KeyCode[] keyCode = keyCommand.GetKeyCode();
            bool flag = keyCode.Length == 1;
            bool result;
            if (flag)
            {
                result = Input.GetKeyDown(keyCode[0]);
            }
            else
            {
                bool flag2 = keyCode.Length == 2;
                result = (flag2 && Input.GetKey(keyCode[0]) && Input.GetKeyDown(keyCode[1]));
            }
            return result;
        }
    }
}
